import pandas as pd
import numpy as np
import numpy.random as nr
from sklearn.cluster import KMeans, AgglomerativeClustering
from sklearn.metrics import silhouette_score
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt

#读取数据
auto_prices = pd.read_csv('../实验数据/Automobile price data _Raw_.csv')

#判断某一列中是否有 ”？“
x=(auto_prices.astype(np.object) == '?').any()
# print(x)

#显示每列的数据类型
print(auto_prices.dtypes)

#有？的列的数据类型为object,我们通过这个特性得出该列的有多少缺失值
for col in auto_prices.columns:
    if auto_prices[col].dtypes == object:
        count=0
        count=[count+1 for x in auto_prices[col] if x=='?' ]
        print(col + ' ' + str(sum(count)))

#删除列normalized-losses
auto_prices.drop('normalized-losses',axis=1,inplace=True)
cols=['bore','stroke','horsepower','peak-rpm','price','num-of-doors']
for column in cols:
    auto_prices.loc[auto_prices[column] == '?',column]=np.nan

df = pd.DataFrame(auto_prices)
# 保存到本地excel
df.to_excel("中间结果.xlsx", index=False)

#删除含nan的行
auto_prices.dropna(axis=0,inplace=True)
print(auto_prices.shape)

df = pd.DataFrame(auto_prices)
# 保存到本地excel
df.to_excel("预处理之后的数据.xlsx", index=False)

